<?php

require_once dirname(__FILE__) . '/../lib/ct_documentoGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/ct_documentoGeneratorHelper.class.php';

/**
 * ct_documento actions.
 *
 * @package    ingenyo-biblio
 * @subpackage ct_documento
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class ct_documentoActions extends autoCt_documentoActions
{

  public function executeIndex(sfWebRequest $request)
  {
    parent::executeIndex($request);
    $this->filters = new CtDocumentoFormFilterCollection();

  }

  public function executeNew(sfWebRequest $request)
  {
    $this->redirect('ct_documento/index');

  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->redirect('ct_documento/index');

  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->redirect('ct_documento/index');

  }

  public function executeBuscar(sfWebRequest $request)
  {
    sfConfig::set('sf_web_debug', false);
    sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
    
    if ((float)$this->getUser()->getAttribute('counter_search',0) > 2){
      $captcha = new securimage();
      if ($captcha->check($request->getParameter('captcha_code'))){
        $this->getUser()->setAttribute('counter_search',0);
      }else{
        return $this->renderText(get_partial('captcha', array('captcha' => $captcha,'error' => true)));
      }
    }
    
    $form = new CtDocumentoFormFilterCollection();
    $form->bind($request->getParameter('filters'));
    $pager = new sfPropelPager('CTDocumento', sfConfig::get('app_max_documentos_en_busqueda'));
    $pager->setCriteria($form->getCriteriaFilter());
    $pager->setPage($request->getParameter('page', 1));

    $pager->init();

    $this->sort = $this->getSort();
    $this->helper = new ct_documentoGeneratorHelper();
    
    
    if ((float)$this->getUser()->getAttribute('counter_search',0) == 2){
      $captcha = true;
    }else{
      $captcha = false;
    }
    
    $this->getUser()->setAttribute('counter_search',(float)$this->getUser()->getAttribute('counter_search',0) + 1);
    
    return $this->renderText(get_partial('result_list', array('pager' => $pager,
          'sort' => $this->sort,
          'configuration' => $this->configuration,
          'helper' => $this->helper,
          'captcha' => $captcha)));

  }

  public function executeAddCustom(sfWebRequest $request)
  {
    $next = $request->getParameter('next');
    $is_the_last = $request->getParameter('is_the_last');
    $form = new CtDocumentoFormFilterCollection();
    $form->addCustom($next, $is_the_last);
    sfConfig::set('sf_web_debug', false);
    sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
    return $this->renderText(get_partial('custom', array('custom_filters' => $form['filters']['custom'][$next],
          'add' => true)));

  }

  public function executeShow(sfWebRequest $request)
  {
    $this->CtDocumento = $this->getRoute()->getObject();
    $this->caracteristicas = $this->CtDocumento->getCaracteristicasFicha();
  }
  
  public function executeCaptchaImage(sfWebRequest $request)
  {
    $captcha = new securimage();
    $captcha->show('captcha/backgrounds/bg3.jpg');
  }

}
